/**
 *
 * @param {string} key
 * @param {*} value
 * @param {*} expire_time 过期时间 单位分钟
 */
export function setCache(key: string, value: any, expire_time: number | boolean) {
  if (expire_time !== true) {
    expire_time = expire_time * 60 * 1000
  }
  const cache = {
    key,
    value,
    expire_time
  }
  localStorage.setItem(key, JSON.stringify(cache))
}

export function getCache(key) {
  const cache = localStorage.getItem(key)
  if (cache) {
    const cache_data = JSON.parse(cache)
    if (cache_data.expire_time === true || cache_data.expire_time > new Date().getTime()) {
      return cache_data.value
    } else {
      localStorage.removeItem(key)
      return null
    }
  }
  return null
}

export function removeCache(key) {
  localStorage.removeItem(key)
}
